﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoolERP_BLL;
using CoolERP_Entities;

namespace CoolERP_Web.User
{
    public partial class RoleEdit : BasePage
    {
        protected Guid id
        {
            get { return this.GetUrlParam("id") == null ? Guid.Empty : new Guid(this.GetUrlParam("id").ToString()); }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (id != Guid.Empty)
                {                  
                    BindData();
                }
            }
        }

        private void BindData()
        {
            using (RolesBLL dao = new RolesBLL())
            {
                Roles role = dao.GetEntity(t => t.Id == id);
                this.tbName.Text = role.Name;
                this.tbDescription.Text = role.Description;
            }
        }

        protected void btnSave_Click(object sender, ImageClickEventArgs e)
        {
            int result = 0;
            using (RolesBLL rBLL = new RolesBLL())
            {
                Roles entity = new Roles();
                if (id != Guid.Empty)
                    entity = rBLL.GetEntity(t => t.Id == id);
                else
                    entity.Id = Guid.NewGuid();
                entity.Name = this.tbName.Text.Trim();
                entity.Description = this.tbDescription.Text.Trim();
                result = rBLL.SaveEntity(entity, t => t.Id != entity.Id && t.Name == entity.Name);
            }
            if (result == 1)
            {
                btnSave.Visible = false;
                lbResult.Text = "保存成功";
            }
            else if (result == -1)
                lbResult.Text = "已存在此角色名称，请重新输入";
            else
                lbResult.Text = "保存失败";
        }
    }
}